/**
 * @Author: zhongyang
 * @Date: 2022/1/25 15:54
 */

package redistools

import (
	"context"
	"errors"
	"mine-info-server/global"
	"time"
)

func Get(key string) (result string, err error) {
	result, err = global.GVA_REDIS.Get(context.Background(), key).Result()
	return result, err
}

func SetString(key string, value string) (err error) {
	timer := time.Duration(global.GVA_CONFIG.Redis.ExpireTime) * time.Hour
	err = global.GVA_REDIS.Set(context.Background(), key, value, timer).Err()
	return err
}

func Delete(key ...string) (err error) {
	err = global.GVA_REDIS.Del(context.Background(), key...).Err()
	return err
}

func Scan(matchName string) []string {
	var cur uint64
	var keysResult []string
	for {
		// 每次扫描20条
		keys, cur, err := global.GVA_REDIS.Scan(context.Background(), cur, matchName, 20).Result()
		if err != nil {
			panic(errors.New("redis出错，请联系管理员"))
		}
		keysResult = append(keysResult, keys...)
		if cur == 0 {
			break
		}
	}
	return keysResult
}
